<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

require_once 'Zend/Controller/Action.php';
require_once APPLICATION_PATH . '/forms/NuevoCliente.php';
require_once APPLICATION_PATH . '/forms/EditarCliente.php';
require_once APPLICATION_PATH . '/forms/EliminarCliente.php';
require_once APPLICATION_PATH . '/forms/MostrarCliente.php';
require_once APPLICATION_PATH . '/forms/BuscarCliente.php';
require_once APPLICATION_PATH . '/forms/ConsultarClientes.php'; 

require_once dirname(dirname(__FILE__)) . '/models/Single/ClienteEntity.php';

/**
 * Description of ClienteController
 *
 * @author kristian
 */
class ClienteController extends Zend_Controller_Action{
    protected $_flashMessenger = null;
    private $destinoRedireccion = '/cliente';

    public function init()
    {
        $this->initView();
        $this->_flashMessenger = $this->_helper->FlashMessenger;
        $this->view->baseUrl = $this->_request->getBaseUrl();
        $this->view->messages = $this->_flashMessenger;
        //Zend_Loader::loadClass('Cliente');
    }

    public function indexAction()
    {
        $this->consultarAction();
    }

    public function consultarAction()
    {
        $this->view->title = "Listado de Clientes";
        $cliente = new Spalm_Model_Single_ClienteEntity();
        $this->view->clientes = $cliente->fetchAll();
    }

    public function nuevoAction()
    {
        $this->view->title = "Agregar nuevo Cliente";
        $form = new NuevoClienteForm();
        $form->setAction($this->view->baseUrl . '/cliente/nuevo');

        if($this->getRequest()->isPost()) {
            $formData = $this->_request->getPost();
            if (!$form->isValid($formData)) {
                $this->view->form = $form;
                $form->populate($formData);
                return $this->render('form');
            }

            try {
                $cliente = new Spalm_Model_Single_ClienteEntity();
                $localidad = new Spalm_Model_Single_LocalidadEntity();

                //arma el objeto de localidades
                $localidad = $localidad->find((int) $form->getValue('localidad'));

                $cliente->setDomicilio($form->getValue('domicilio'));
                $cliente->setEmail($form->getValue('email'));
                $cliente->setNroTelefono($form->getValue('nroTelefono'));
                $cliente->setNumeroDocumento($form->getValue('nroDocumento'));
                $cliente->setRazonSocial('razonSocial');
                $cliente->setTipoDocumento($form->getValue('tipoDocumento'));
                $cliente->setLocalidad($localidad);


                $cliente->save();

                $this->_flashMessenger->addMessage('El cliente ha sido guardado con &eacute;xito');
                $this->_redirect($this->destinoRedireccion);

            } catch (Exception $e) {
                $this->_flashMessenger->addMessage($e->getMessage());
            }
        }

        $this->view->form = $form;
        $this->render('form');
    }

    public function editarAction()
    {
        $id = (int)$this->_request->getParam('id', 0);
        $this->view->title = "Editar Cliente";
        

        $cliente = new Spalm_Model_Single_ClienteEntity();
        $cliente = $cliente->find($id);

        $form = new NuevoClienteForm($cliente,true);
        $form->setAction($this->view->baseUrl . '/cliente/editar/id/' . $id);

        if (null === $cliente) {
            $this->_flashMessenger->addMessage('Este Cliente no existe');
            $this->_redirect($this->destinoRedireccion);
        }

        if ($this->_request->isPost())
        {
            $formData = $this->_request->getPost();
            if (!$form->isValid($formData)) {
                $this->view->form = $form;
                $form->populate($formData);
                return $this->render('form');
            }

            try {
                $localidad = new Spalm_Model_Single_LocalidadEntity();

                //arma el objeto de localidades
                $localidad = $localidad->find((int) $form->getValue('localidad'));
                $cliente = $cliente->find( (int) $form->getValue('cliente'));

                $cliente->setDomicilio($form->getValue('domicilio'));
                $cliente->setEmail($form->getValue('email'));
                $cliente->setNroTelefono($form->getValue('nroTelefono'));
                $cliente->setNumeroDocumento($form->getValue('nroDocumento'));
                $cliente->setRazonSocial('razonSocial');
                $cliente->setTipoDocumento($form->getValue('tipoDocumento'));
                $cliente->setLocalidad($localidad);


                $cliente->save();

                $this->_flashMessenger->addMessage('El cliente ha sido guardado con &eacute;xito');
                $this->_redirect($this->destinoRedireccion);

            } catch (Exception $e) {
                $this->_flashMessenger->addMessage($e->getMessage());
            }
        }

        $this->view->cliente = $cliente;
        $form->populate($this->view->cliente->toArray());
        $this->view->form = $form;
        $this->render('form');
    }

    public function eliminarAction()
    {
        $id = (int)$this->_request->getParam('id', 0);

        $cliente = new Spalm_Model_Single_ClienteEntity();
        $cliente = $cliente->find($id);

        if (null !== $cliente) {
            try {

                $cliente->remove();

                $this->_flashMessenger->addMessage('El cliente ha sido eliminado con &eacute;xito');
            } catch (Exception $e) {
                $this->_flashMessenger->addMessage($e->getMessage());
            }
        } else{
            $this->_flashMessenger->addMessage('Este Cliente no existe');
        }

        $this->_redirect($this->destinoRedireccion);
    }

    public function mostrarAction() {
         $this->view->title = "Ver detalle del Cliente";


        $cliente = new Spalm_Model_Single_ClienteEntity();
        $cliente = $cliente->find($id);

        $form = new NuevoClienteForm($cliente);
    }

    public function buscarAction() {
        $this->view->title = "Buscar Clientes";

        $form = new BuscarCliente();

        if ($this->_request->isPost())
        {
            $formData = $this->_request->getPost();
            if (!$form->isValid($formData)) {
                $this->view->form = $form;
                $form->populate($formData);
                return $this->render('form');
            }

            $valores = $form->getValues();

            $aBuscar = $valores['buscado'];
            $criterio = $valores['criterio'];

            $clienteModel = new Spalm_Model_Single_ClienteEntity();
            $clienteRows = $clienteModel->getMapper()->getDbTable()->find("$criterio = $aBuscar");
            
            $this->_redirect($this->destinoRedireccion . '/consulta');
        }
    }
}
?>
